home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2 (Special) / PCPro-2b.iso / Demos / Macromedia / CourseBuilder / CourseBuilderInstaller.exe / Disk1 / data1.cab / Dreamweaver-unInstalled / Configuration / Commands / Convert From Prev Versions.js < prev    next >
Encoding:
JavaScript  |  1999-12-06  |  21.9 KB  |  642 lines

  1. // Copyright 1999 Macromedia, Inc. All rights reserved.
  2.  
  3. //*************** GLOBALS VARS *****************
  4.  
  5. var TAG_NAME = "{#CUSTOMOBJ}"
  6.  
  7. var PAGE = '';
  8.  
  9. var MAKE_BACKUP = true;
  10.  
  11. var LOG_INFO = '';
  12. var HTML_START = '<html><head><title>%s</title></head><body>\n';
  13. var HTML_STOP = '</body></html>';
  14. var LOG_TITLE = '<center><tt><font size=5><b>%s</b></font><br>%s</tt></center>\n<br><br>\n';
  15. var SECTION_START = '<tt><font size=4><b>%s</b></font></tt><br><ul>\n';
  16. var SECTION_STOP  = '</ul>\n';
  17. var SECTION_ITEM = '<li>%s</li>\n';
  18. var SECTION_ITEM_ERROR = '<br><font color="red">%s</font>\n';
  19. var HTML_ERROR = '<font color="red">%s</font>';
  20. var ERRORS = 0;
  21.  
  22. var DOM_OBJ = '';
  23.  
  24. //******************* API **********************
  25.  
  26. function canAcceptCommand() {
  27.   if (documentSaved(true)) return true;
  28.   else return false;
  29. }
  30.  
  31. //***************** LOCAL FUNCTIONS  ******************
  32.  
  33.  
  34. //Convert From Previous Versions
  35. //
  36. function convert() {
  37.   DOM_OBJ = dreamweaver.getDocumentDOM();
  38.   
  39.   convertCustomObjects();
  40.   
  41.   addSection(MSG_secDocConvert);
  42.   
  43.   convertAttainObjIds();
  44.   convertAttainKos();
  45.   convertFns();
  46.   convertWriteToFns();
  47.   convertTemplateRefs();
  48.   convertQuotes();
  49.   // convertGizmos();
  50.   updatePage(); 
  51.   
  52.   endSection();
  53. }
  54.  
  55.  
  56. //Convert from custom objects
  57. //
  58. function convertCustomObjects() {
  59.   var customList, i, obj, objSrc, intId, name;
  60.   var intList, includeSrc, scriptList, script, source, am;
  61.   var iSrcList, divList, divObj, index;
  62.   
  63.   var DOM_OBJ = dreamweaver.getDocumentDOM();
  64.   
  65.   customList = DOM_OBJ.getElementsByTagName(TAG_NAME);
  66.   
  67.   if (customList.length > 0) {
  68.     
  69.     addSection(MSG_secConvertTags);
  70.     
  71.     for (var i=0; i < customList.size; i++) {
  72.       obj = customList.item(i);
  73.       name = obj.getAttribute("NAME");
  74.       
  75.       addItem(MSG_itemConverting + name);
  76.       
  77.       objSrc = obj.outerHTML;
  78.       intId = objSrc.match(/KO_int\s*\(\s*['"](K\d\d)['"]/);
  79.       if (intId != null) 
  80.         intId = intId[1];
  81.       else {
  82.         addError(MSG_intIdNotFound, true);
  83.         continue;
  84.       }
  85.       
  86.       // move behavior information outside of the KO
  87.       var behInfo = '', result;
  88.       var behCall = /<!--\s*#BeginBehavior\s+(\w+)\s*-->[^\x00]*<!--\s*#EndBehavior\s+(\1)\s*-->/;
  89.       while ((result = behCall.exec(objSrc)) != null) {
  90.         behInfo += result[0];
  91.         objSrc = objSrc.substring(0, result.index) + objSrc.substring(result.index + result[0].length);
  92.       }
  93.       
  94.       // replace the custom object tags
  95.       objSrc = objSrc.replace(/<!--\s*#BeginCustomObject/, "<interaction object=\""+intId+"\"");
  96.       objSrc = objSrc.replace(/\s*-->/, ">");
  97.       objSrc = objSrc.replace(/<!--\s*#EndCustomObject\s*-->/,"<cbi-select object=\""+intId+"\"></interaction>");
  98.       obj.outerHTML = NEWLINE + objSrc + NEWLINE + behInfo + NEWLINE;
  99.     }
  100.     
  101.     endSection();
  102.  
  103.     // now find all the Interaction objects, and continue the conversion
  104.     
  105.     intList = DOM_OBJ.getElementsByTagName("INTERACTION");
  106.     for (i=0; i < intList.size; i++) {
  107.       obj = intList.item(i);
  108.       intId = obj.getAttribute(OBJID_ATTR);
  109.       name = obj.getAttribute(NAME_ATTR);
  110.       
  111.       addSection(MSG_secUpdating + name);
  112.       
  113.       // Remove the wizard and type attributes
  114.       addItem(MSG_itemDelAttrs);
  115.       obj.removeAttribute("wizard");
  116.       obj.removeAttribute("type");
  117.  
  118.       // Remove script includes, creating an include source list
  119.       addItem(MSG_itemScripts);
  120.       includeSrc = '';
  121.       scriptList = obj.getElementsByTagName("SCRIPT");
  122.       for (var j=0; j < scriptList.size; j++) {
  123.         script = scriptList.item(j);
  124.         if (script.getAttribute("src") != null) {  //convert include
  125.           source = script.getAttribute("src");
  126.           if (script.innerHTML.indexOf('if') == -1) { //add it to includeSrc
  127.             source = source.substring(source.lastIndexOf('/')+1);
  128.             includeSrc += ((includeSrc.length == 0)?'':',') + source;
  129.           }
  130.           script.outerHTML = '';  //remove the include
  131.         } else if (script.innerHTML.indexOf("KO_int(") != -1) {  //update init code
  132.           addItem(MSG_itemAMConvert);
  133.           script.setAttribute("object", intId);
  134.           // remove init stuff
  135.           am = script.innerHTML;
  136.           am = am.replace(/\s*with\s+\(navigator\)(.|\n)*\/\/-->/, '\n//-->');
  137.           script.innerHTML = am;
  138.         } else if (script.getAttribute("name") == "cmifrag") {
  139.           script.outerHTML = '';  // remove the CMI init code
  140.       } }
  141.       obj.setAttribute(INC_SRC_ATTR, includeSrc);
  142.  
  143.       // Add a <div> tag around the HTML portion of the KO
  144.       // This should not include any existing layers.
  145.       // This tag will be used to insert the KO in a layer.
  146.       addItem(MSG_itemAddDiv);
  147.       objSrc = obj.innerHTML;
  148.       var index = objSrc.search(/\<script[^>]*object=["']K\d\d["']/i);
  149.       if (index != -1) {
  150.         obj.innerHTML = "<div name=\""+ intId +"Layer\">" + NEWLINE +
  151.                         objSrc.substring(0, index) +
  152.                         "</div>" + NEWLINE +
  153.                         objSrc.substring(index);
  154.         divList = obj.getElementsByTagName("DIV");
  155.         // find the new div
  156.         for (var j=0; j < divList.size; j++) {
  157.           if (divList.item(j).getAttribute("name") == intId + "Layer") {
  158.             divObj = divList.item(j);
  159.             break;
  160.         } }
  161.         // now move non-nested layers outside the new div
  162.         divList = divObj.getElementsByTagName("DIV");
  163.         var divHTML = '';
  164.         for (var j=0; j < divList.size; j++) {
  165.           if (isLayer(divList.item(j))) {
  166.             var parent = divList.item(j);
  167.             while (parent.parentNode != divObj && parent != null)
  168.               parent = parent.parentNode;
  169.             if (parent == null) parent = divList.item(j);
  170.             divHTML += parent.outerHTML;
  171.             parent.outerHTML = '';
  172.             divList = divObj.getElementsByTagName("DIV");
  173.             j--;
  174.         } }
  175.         if (divHTML) divObj.outerHTML = divObj.outerHTML + divHTML;
  176.       }
  177.       
  178.       endSection();
  179.   } }
  180. }
  181.  
  182.  
  183. //Convert Kxx to Gxx.  Convert KO_int to AttainObj. Convert Gizmo to AttainObj.
  184. //
  185. function convertAttainObjIds() {
  186.   var docSrc;
  187.   addItem(MSG_itemConvertIds);
  188.   docSrc = DOM_OBJ.documentElement.innerHTML;
  189.   docSrc = docSrc.replace(/new KO_int/g, "new MM_interaction");
  190.   docSrc = docSrc.replace(/new AttainObj/g, "new MM_interaction");
  191.   docSrc = docSrc.replace(/K(\d\d)/g, "G$1");
  192.   if (DOM_OBJ.documentElement.innerHTML != docSrc)
  193.     DOM_OBJ.documentElement.innerHTML = docSrc;
  194. }
  195.  
  196.  
  197. //Convert <attain-ko>, <attain-select>, <attain-object> and <attain-am> tags to 
  198. //  <interaction> and <cbi-select>.
  199. //
  200. function convertAttainKos() {
  201.   var bodySrc, amList, i, obj, attainObjId;
  202.   addItem(MSG_itemConvertTags);
  203.   bodySrc = DOM_OBJ.body.outerHTML;
  204.   bodySrc = bodySrc.replace(/attain-ko/gi, "interaction");
  205.   bodySrc = bodySrc.replace(/attain-object/gi, "interaction");
  206.   bodySrc = bodySrc.replace(/attain-select/gi, "cbi-select");
  207.   if (DOM_OBJ.body.outerHTML != bodySrc)
  208.     DOM_OBJ.body.outerHTML = bodySrc;
  209.   
  210.   // Convert beta <attain-am> tags to <cbi-select> tags
  211.   amList = DOM_OBJ.getElementsByTagName("ATTAIN-AM");
  212.   if (amList.length > 0) {
  213.     addItem(MSG_itemAddSelectIcon);
  214.     for (i=0; i < amList.size; i++) {
  215.       obj = amList.item(i);
  216.       attainObjId = obj.getAttribute(OBJID_ATTR);
  217.       obj.outerHTML = obj.innerHTML + NEWLINE + "<cbi-select object=\""+attainObjId+"\">";  
  218.   } }
  219. }
  220.  
  221.  
  222. //Convert behavior calls and js includes.
  223. //
  224. function convertFns() {
  225.   var docSrc;
  226.   
  227.   addItem(MSG_itemConvertFunctions);
  228.   
  229.   // remove any existing function declarations
  230.   PAGE.scriptDOM.removeFunction("MM_initKO");
  231.   PAGE.scriptDOM.removeFunction("MM_judgeKO");
  232.   PAGE.scriptDOM.removeFunction("MM_resetKO");
  233.   PAGE.scriptDOM.removeFunction("MM_setKOProperties");
  234.   
  235.   PAGE.scriptDOM.removeFunction("MM_initAttainObj");
  236.   PAGE.scriptDOM.removeFunction("MM_judgeAttainObj");
  237.   PAGE.scriptDOM.removeFunction("MM_resetAttainObj");
  238.   PAGE.scriptDOM.removeFunction("MM_setAttainObjProps");
  239.   
  240.   // now change the function and include names
  241.   docSrc = DOM_OBJ.documentElement.innerHTML;
  242.   docSrc = docSrc.replace(/behKnowledgeActions\.js/g, "behCourseBuilder.js");
  243.   docSrc = docSrc.replace(/intClass\.js/g, "interactionClass.js");
  244.   docSrc = docSrc.replace(/MM_initKO\(/g, "MM_initInteractions(");
  245.   docSrc = docSrc.replace(/MM_judgeKO\(/g, "MM_judgeInt(");
  246.   docSrc = docSrc.replace(/MM_resetKO\(/g, "MM_resetInt(");
  247.   docSrc = docSrc.replace(/MM_setKOProperties\(/g, "MM_setIntProps(");
  248.   docSrc = docSrc.replace(/initKoFns/g, "MM_initIntFns");
  249.   
  250.   docSrc = docSrc.replace(/behAttainObjects\.js/g, "behCourseBuilder.js");
  251.   docSrc = docSrc.replace(/attainObjClass\.js/g, "interactionClass.js");
  252.   docSrc = docSrc.replace(/MM_initAttainObj\(/g, "MM_initInteractions(");
  253.   docSrc = docSrc.replace(/MM_judgeAttainObj\(/g, "MM_judgeInt(");
  254.   docSrc = docSrc.replace(/MM_resetAttainObj\(/g, "MM_resetInt(");
  255.   docSrc = docSrc.replace(/MM_setAttainObjProps\(/g, "MM_setIntProps(");
  256.   docSrc = docSrc.replace(/initAttainObjFns/g, "MM_initIntFns");
  257.   
  258.   docSrc = docSrc.replace(/KO_findObject\(/g, "MM_intFindObject(");
  259.   docSrc = docSrc.replace(/KO_getDocProp\(/g, "MM_getDocProp(");
  260.   docSrc = docSrc.replace(/KA_preloadImages/g, "MM_preloadImages");
  261.   
  262.   docSrc = docSrc.replace(/AO_findObject\(/g, "MM_intFindObject(");
  263.   docSrc = docSrc.replace(/AO_getDocProp\(/g, "MM_getDocProp(");
  264.   
  265.   // change the nice names in the action manager
  266.   docSrc = docSrc.replace(/(['"]actn['"]\s*,\s*['"])Judge(["':])/g, "$1Judge Interaction$2");
  267.   docSrc = docSrc.replace(/(['"]actn['"]\s*,\s*['"])Reset(["':])/g, "$1Reset Interaction$2");
  268.   docSrc = docSrc.replace(/(['"]actn['"]\s*,\s*['"])Set Properties(["':])/g, "$1Set Interaction Properties$2");
  269.   
  270.   docSrc = docSrc.replace(/(['"]actn['"]\s*,\s*['"])Control Sound(["':])/g, "$1Play Sound$2");
  271.   docSrc = docSrc.replace(/(['"]actn['"]\s*,\s*['"])Judge Attain Object(["':])/g, "$1Judge Interaction$2");
  272.   docSrc = docSrc.replace(/(['"]actn['"]\s*,\s*['"])Reset Attain Object(["':])/g, "$1Reset Interaction$2");
  273.   docSrc = docSrc.replace(/(['"]actn['"]\s*,\s*['"])Set Attain Properties(["':])/g, "$1Set Interaction Properties$2");
  274.   
  275.   
  276.   if (DOM_OBJ.documentElement.innerHTML != docSrc)
  277.     DOM_OBJ.documentElement.innerHTML = docSrc;
  278. }
  279.  
  280.  
  281. //Convert Write To Behaviors to Set Text
  282. //
  283. function convertWriteToFns() {
  284.   var docSrc;
  285.   
  286.   addItem(MSG_itemConvertWriteToFns);
  287.   
  288.   // remove any existing function declarations
  289.   PAGE.scriptDOM.removeFunction("MM_replaceLayerText");
  290.   PAGE.scriptDOM.removeFunction("MM_writeToLayer");
  291.   PAGE.scriptDOM.removeFunction("MM_writeToFrame");
  292.   PAGE.scriptDOM.removeFunction("MM_writeToPopup");
  293.   PAGE.scriptDOM.removeFunction("MM_writeToTextfield");
  294.   
  295.   // now change the function and include names
  296.   docSrc = DOM_OBJ.documentElement.innerHTML;
  297.   docSrc = docSrc.replace(/MM_replaceLayerText\(/g, "MM_setTextOfLayer(");
  298.   docSrc = docSrc.replace(/MM_writeToLayer\(/g, "MM_setTextOfLayer(");
  299.   docSrc = docSrc.replace(/MM_writeToFrame\(/g, "MM_setTextOfFrame(");
  300.   docSrc = docSrc.replace(/MM_writeToPopup\(/g, "MM_popupMsg(");
  301.   docSrc = docSrc.replace(/MM_writeToTextfield\(/g, "MM_setTextOfTextfield(");
  302.   
  303.   // change the nice names in the action manager
  304.   docSrc = docSrc.replace(/(['"]actn['"]\s*,\s*['"])Replace Layer Text(["':])/g, "$1Set Text of Layer$2");
  305.   docSrc = docSrc.replace(/(['"]actn['"]\s*,\s*['"])Write To Popup(["':])/g, "$1Popup Message$2");
  306.   docSrc = docSrc.replace(/(['"]actn['"]\s*,\s*['"])Write To Layer(["':])/g, "$1Set Text of Layer$2");
  307.   docSrc = docSrc.replace(/(['"]actn['"]\s*,\s*['"])Write To Frame(["':])/g, "$1Set Text of Frame$2");
  308.   docSrc = docSrc.replace(/(['"]actn['"]\s*,\s*['"])Write To Text Field(["':])/g, "$1Set Text of Text Field$2");
  309.   docSrc = docSrc.replace(/(['"]actn['"]\s*,\s*['"])Display Status Message(["':])/g, "$1Set Text of Status Bar$2");
  310.   
  311.   if (DOM_OBJ.documentElement.innerHTML != docSrc)
  312.     DOM_OBJ.documentElement.innerHTML = docSrc;
  313. }
  314.  
  315.  
  316. function convertTemplateRefs() {
  317.   var bodySrc;  
  318.   addItem(MSG_itemConvertTemplates);
  319.   bodySrc = DOM_OBJ.body.outerHTML;
  320.   bodySrc = bodySrc.replace(/\.kot/g, ".agt");
  321.   if (DOM_OBJ.body.outerHTML != bodySrc)
  322.     DOM_OBJ.body.outerHTML = bodySrc;
  323. }
  324.  
  325.  
  326. //Convert from double to single quotes in the action manager
  327. //
  328. function convertQuotes() {
  329.   var scriptList, i, j, obj, objSrc, newSrc, lastIndex, inString;
  330.   scriptList = DOM_OBJ.getElementsByTagName("SCRIPT");
  331.   if (scriptList.length > 0) {
  332.     addItem(MSG_convertQuotes);
  333.     for (i=0; i < scriptList.size; i++) {
  334.       obj = scriptList.item(i);
  335.       if (obj.getAttribute("SRC") == null && 
  336.           obj.innerHTML.indexOf("MM_interaction") != -1 &&
  337.           obj.innerHTML.search(/function\s+newG\d\d\s*\(/) != -1) {
  338.         objSrc = obj.innerHTML;
  339.         inString = false; lastIndex = 0; newSrc = '';
  340.         for (j=0;  j < objSrc.length; j++) {
  341.           if (objSrc.charAt(j) == '"' && (j == 0 || objSrc.charAt(j-1) != '\\')) {
  342.             inString = !inString;            
  343.             newSrc += objSrc.substring(lastIndex, j) + "'";
  344.             lastIndex = j+1;
  345.           }
  346.           if (inString && objSrc.charAt(j) == "'" && objSrc.charAt(j-1) != '\\') {
  347.             newSrc += objSrc.substring(lastIndex, j) + "\\'";
  348.             lastIndex = j+1;
  349.           }
  350.         }
  351.         if (lastIndex != 0) {
  352.           newSrc += objSrc.substring(lastIndex);
  353.           if (newSrc != obj.innerHTML)
  354.             obj.innerHTML = newSrc;
  355.   } } } }    
  356. }
  357.  
  358.  
  359. /*
  360. //Convert <gizmo> to <attain-object>, <gizmo-select> to <attain-selelct>, etc.
  361. //
  362. function convertGizmos() {
  363.   var bodySrc, amList, i, obj, attainObjId;
  364.   addItem(MSG_itemConvertTags);
  365.   bodySrc = DOM_OBJ.body.outerHTML;
  366.   bodySrc = bodySrc.replace(/new Gizmo/g, "new AttainObj");
  367.   bodySrc = bodySrc.replace(/<gizmo-select/gi, "<attain-select");
  368.   bodySrc = bodySrc.replace(/<gizmo/gi, "<attain-object");
  369.   bodySrc = bodySrc.replace(/<\/gizmo/gi, "</attain-object");
  370.   if (DOM_OBJ.body.outerHTML != bodySrc)
  371.     DOM_OBJ.body.outerHTML = bodySrc;
  372.  
  373.   // now change the function and include names
  374.   docSrc = DOM_OBJ.documentElement.innerHTML;
  375.   docSrc = docSrc.replace(/behAttainGizmos\.js/g, "behAttainObjects.js");
  376.   docSrc = docSrc.replace(/gizmoClass\.js/g, "attainObjClass.js");
  377.   docSrc = docSrc.replace(/MM_initGizmo\(/g, "MM_initAttainObj(");
  378.   docSrc = docSrc.replace(/MM_judgeGizmo\(/g, "MM_judgeAttainObj(");
  379.   docSrc = docSrc.replace(/MM_resetGizmo\(/g, "MM_resetAttainObj(");
  380.   docSrc = docSrc.replace(/MM_setGizmoProperties\(/g, "MM_setAttainObjProps(");
  381.   docSrc = docSrc.replace(/initGizmoFns/g, "initAttainObjFns");
  382.   docSrc = docSrc.replace(/AG_findObject\(/g, "AO_findObject(");
  383.   docSrc = docSrc.replace(/AG_getDocProp\(/g, "AO_getDocProp(");
  384.   
  385.   // change the nice names in the action manager
  386.   docSrc = docSrc.replace(/(['"]actn['"]\s*,\s*['"])Reset Gizmo(["':])/g, "$1Reset Attain Object$2");
  387.   docSrc = docSrc.replace(/(['"]actn['"]\s*,\s*['"])Judge Gizmo(["':])/g, "$1Judge Attain Object$2");
  388.   docSrc = docSrc.replace(/(['"]actn['"]\s*,\s*['"])Set Gizmo Properties(["':])/g, "$1Set Attain Properties$2");
  389.   docSrc = docSrc.replace(/Disable Gizmo/g, "Disable Object");
  390.   
  391.   if (DOM_OBJ.documentElement.innerHTML != docSrc)
  392.     DOM_OBJ.documentElement.innerHTML = docSrc;
  393. }
  394. */
  395.  
  396.  
  397. function updatePage() {
  398.   // Add initialization functions and events
  399.   addItem(MSG_itemAddInit);
  400.   PAGE.updateAttainObjInit();
  401.  
  402.   addItem(MSG_itemAddBeh);
  403.   PAGE.insertBehaviorFns(DOM_OBJ.body.outerHTML);
  404.   
  405.   // Add script includes to the head of the document
  406.   addItem(MSG_itemAddScripts);
  407.   PAGE.insertAttainObjIncludes();
  408.   PAGE.updateCmiIncludes();
  409.  
  410.   // Remove any functions which are now part of the includes
  411.   addItem(MSG_itemRemoveFns);
  412.   PAGE.removeRedundantFns();
  413.   PAGE.removeEmptyAttainObjs();
  414. }
  415.  
  416. function initializeUI() {
  417.   var obj, theSel, intId = '', oldFile;
  418.   var docDOM = dw.getDocumentDOM();
  419.   var stopConvert = false;
  420.   
  421.   if (!regCheck()) {
  422.     window.close();
  423.     return;
  424.   }
  425.   
  426.   
  427.   setBusyCursor();
  428.   
  429.   setNewline(); //init NEWLINE global
  430.  
  431.   // create the page object
  432.   PAGE = new AttainDOM();  
  433.   
  434.   startLog(LABEL_convertLog, LABEL_convertLog, dreamweaver.getDocumentPath("document"));
  435.   
  436.   // check that the scripts exist
  437.   if (needToCopySupportFiles()) {
  438.     if (confirm(MSG_overwriteFiles)) {
  439.       copySupportFiles(false, true, true);
  440.       if (needToCopySupportFiles()) {
  441.         alert(MSG_scriptsNotUpdated);
  442.         stopConvert = true;
  443.       } else {
  444.         addHtml(MSG_copiedSupportFiles + "<br><br>");
  445.       }
  446.     } else {
  447.       addHtml(MSG_supportFilesNotCopied + "<br><br>");
  448.     }
  449.   } else {
  450.     addHtml(MSG_supportFilesCurrent + "<br><br>");
  451.   }
  452.     
  453.   // remove uneeded script files: intClass.js, behKnowledgeActions.js
  454.   if (!stopConvert) {
  455.     oldFile = new File(PREF_scriptsUrl + File.separator + "intClass.js");
  456.     if (oldFile.exists() && !oldFile.remove()) 
  457.       addHtmlError(errMsg(MSG_fileNotRemoved, oldFile.getAbsolutePath()) + "<br><br>");
  458.     oldFile = new File(PREF_scriptsUrl + File.separator + "behKnowledgeActions.js");
  459.     if (oldFile.exists() && !oldFile.remove())
  460.         addHtmlError(errMsg(MSG_fileNotRemoved, oldFile.getAbsolutePath()) + "<br><br>");
  461.         
  462.     oldFile = new File(PREF_scriptsUrl + File.separator + "attainObjClass.js");
  463.     if (oldFile.exists() && !oldFile.remove()) 
  464.       addHtmlError(errMsg(MSG_fileNotRemoved, oldFile.getAbsolutePath()) + "<br><br>");
  465.     oldFile = new File(PREF_scriptsUrl + File.separator + "behAttainObjects.js");
  466.     if (oldFile.exists() && !oldFile.remove())
  467.         addHtmlError(errMsg(MSG_fileNotRemoved, oldFile.getAbsolutePath()) + "<br><br>");
  468.   }
  469.   
  470.   // create a backup
  471.   if (!stopConvert && MAKE_BACKUP) {
  472.     if (!createBackup()) {
  473.       if (!confirm(MSG_unableToMakeBackup))
  474.         stopConvert = true;
  475.       else
  476.         addHtml(MSG_backupNotCreated + "<br><br>");
  477.     } else
  478.       addHtml(MSG_createdBackup + dreamweaver.getDocumentPath("document")+".bak<br><br>");
  479.   }
  480.   
  481.   if (!stopConvert) {
  482.     
  483.     addHtml(MSG_updatingFile + dreamweaver.getDocumentPath("document") + "<br><br>");
  484.  
  485.     // Get the currently selected custom obj id
  486.     obj = getCustomObj();
  487.     if (obj) {
  488.       intId = obj.outerHTML.match(/KO_int\s*\(\s*['"]K(\d\d)['"]/);
  489.       if (intId != null) intId = "G" + intId[1];
  490.     } else {
  491.       obj = getOldAttainObj();
  492.       if (obj) {
  493.         intId = obj.outerHTML.match(/KO_int\s*\(\s*['"]K(\d\d)['"]/);
  494.         if (intId != null) {
  495.           intId = "G" + intId[1];
  496.         } else {
  497.           intId = obj.getAttribute(OBJID_ATTR);
  498.     } } }
  499.       
  500.  
  501.     convert();
  502.  
  503.     addHtml("<br><br>" + MSG_convertDone);
  504.  
  505.     saveLog();
  506.  
  507.     // Re-select the original custom object, if it exists
  508.     if (intId) {
  509.       obj = getAttainObj(intId);
  510.       theSel = docDOM.nodeToOffsets(obj);
  511.       docDOM.setSelection(theSel[0], theSel[1]);
  512.     } else {
  513.       docDOM.setSelection(0,0);
  514.     }
  515.     
  516.   }
  517.   
  518.   garbageCollect(false);
  519.   clearBusyCursor();
  520.   window.close();
  521.   
  522.   if (!stopConvert) {
  523.     // Indicate results
  524.     var message = (ERRORS) ? MSG_errorsGenerated : MSG_success;
  525.     if (confirmCustom(message, LABEL_ok, LABEL_viewLog) == LABEL_viewLog)
  526.       dreamweaver.browseDocument(FILE_convertLog);
  527.   }
  528.   
  529.   window.close(); // DW BUG: call again to prevent hang
  530. }
  531.  
  532.  
  533. //Creates a backup file of the current document
  534. // Return true if successful
  535. //
  536. function createBackup() {
  537.   var docSrc, fileName, file, result;
  538.   var retVal = false;
  539.   
  540.   docSrc = dreamweaver.getDocumentDOM().documentElement.outerHTML;
  541.   fileName = dreamweaver.getDocumentPath("document") + ".bak";
  542.   
  543.   file = new File(fileName);
  544.   if (file.exists()) {
  545.     result = file.remove();
  546.     if (!result) return retVal;
  547.   }
  548.  
  549.   result = file.setContents(docSrc);
  550.   if (result) retVal = true;
  551.   
  552.   return retVal;
  553. }
  554.  
  555.  
  556. //Returns the currently selected custom object, or null if it
  557. // does not exist.
  558. //
  559. function getCustomObj() {
  560.   var theSel, theObj;
  561.   var docDOM = dw.getDocumentDOM();
  562.   theSel = docDOM.getSelection();
  563.   theObj = docDOM.offsetsToNode(theSel[0],theSel[1]);
  564.   while (theObj != null) {
  565.     if (theObj.nodeType == Node.ELEMENT && 
  566.         theObj.tagName == "{#CUSTOMOBJ}" &&
  567.         theObj.getAttribute("wizard") == "Knowledge Object")
  568.       break;
  569.     theObj = theObj.parentNode;
  570.   }
  571.   if (theObj)
  572.     return theObj;
  573.   else
  574.     return null;
  575. }
  576.  
  577.  
  578. //Returns the currently selected custom object, or null if it
  579. // does not exist.
  580. //
  581. function getOldAttainObj() {
  582.   var theSel, theObj;
  583.   var docDOM = dw.getDocumentDOM();
  584.   theSel = docDOM.getSelection();
  585.   theObj = docDOM.offsetsToNode(theSel[0],theSel[1]);
  586.   while (theObj != null) {
  587.     if (theObj.nodeType == Node.ELEMENT && 
  588.         (theObj.tagName == "ATTAIN-KO" ||
  589.          theObj.tagName == "ATTAIN-OBJECT") )
  590.       break;
  591.     theObj = theObj.parentNode;
  592.   }
  593.   if (theObj)
  594.     return theObj;
  595.   else
  596.     return null;
  597. }
  598.  
  599.  
  600. function startLog(title, header, filePath) {
  601.   var date = new Date;
  602.   var file = new File(filePath);
  603.   LOG_INFO  = errMsg(HTML_START, title + LABEL_logSeparator + file.getName());
  604.   LOG_INFO += errMsg(LOG_TITLE, header, date);  
  605. }
  606.  
  607. function addHtml(message) {
  608.   LOG_INFO += message;
  609. }
  610.  
  611. function addHtmlError(message) {
  612.   LOG_INFO += errMsg(HTML_ERROR, message);
  613.   ERRORS++;
  614. }
  615.  
  616. function addSection(name) {
  617.   LOG_INFO += errMsg(SECTION_START, name);
  618.   saveLog();
  619. }
  620.  
  621. function addItem(message) {
  622.   LOG_INFO += errMsg(SECTION_ITEM, message);
  623.   saveLog();
  624. }
  625.  
  626. function addError(message) {
  627.   LOG_INFO += errMsg(SECTION_ITEM_ERROR, message);
  628.   ERRORS++;
  629.   saveLog
  630. }
  631.  
  632. function endSection() {
  633.   LOG_INFO += SECTION_STOP;
  634.   saveLog();
  635. }
  636.  
  637. function saveLog() {
  638.   var file = new File(FILE_convertLog);
  639.   file.setContents(LOG_INFO + HTML_STOP);
  640. }
  641.  
  642.